/**
*用户栏目权限设置
*/
var setColPermissions = {
		kindEditerDialog : null,
		divS : '<div id="tcolPerm_dataGrid">',
		divE : '</div><div id="tcolPerm_dataGrid_right"></div>',
		loadColumnSource : function(){
			if(this.getUser().size() > 0){
				this.createDialog();
				var html = '';
				$.ajax({
					url:'/CMS/main/websiteinfoSource',
					type:'post',
					dataType:'json',
					success:function(data){
							html += '<div style="border:1px solid #ffffff;padding:4px;width:100%;">选择网站:';
							html += '<select id="web_select" onchange="setColPermissions.getColumnSource(this,\'/CMS\',1)" style="width:150px;font-size:14px;color:#5E5E5E" ><option value="" disabled="disabled" selected="selected">----站点列表----</option>';
							$.each(data,function(i,u){
									html += '<option value="'+u.id+'">'+u.ftitle+'</option>';
							});
							html += '</select><span style="margin-left:200px;">当前栏目操作权限</span></div>';
							setColPermissions.setDialogHtml(html + setColPermissions.divS + setColPermissions.divE , 'column_source_div');		
					},
					error:function(msg){
						$.growlUI('网站数据加载出错');
					}
				});
			}else{
				$.growlUI('请先选中选项');
			}
		},
		loadPermColumnSource : function(){
			var wid = $("#web_select").val();
			if(wid != ''){
				$.ajax({
					url:'/CMS/main/userColumnIndexSource',
					type:'post',
					data:{'webid':wid,'userId':this.getUser().val(),'check':'0'},
					dataType:'json',
					success:function(data){
						var html = '';
						if(data != ''){
							html = $.fn.simpleTree(data,"/CMS",true,$("#web_select :selected").text(),'permlink');
						}else{
							html = '<div style="width:140px;height:25px;font-size:13px;margin-left:auto;margin-right:auto;margin-top:100px;">未设置可操作栏目！</div>';
						}
							$("#tcolPerm_dataGrid_right").html(html);
						_waiting._hide();	
					},
					error:function(msg){
						$.growlUI('当前可操作栏目加载出错');
						_waiting._hide();
					}
				});
			}
		},
		getColumnSource : function(obj,local){
			if(obj.value != ''){
				_waiting._show();
				$.ajax({
					url:'/CMS/main/tcolunmSource',
					type:'post',
					data:{'webid':obj.value,'type':1},
					dataType:'json',
					success:function(data){
						var html =	$.fn.simpleTree(data,local,true,obj.options[obj.selectedIndex].text,'link');
						setColPermissions.setDialogHtml(html , 'tcolPerm_dataGrid');
						
						setColPermissions.loadPermColumnSource();
					},
					error:function(msg){
						$.growlUI('栏目加载出错');
						_waiting._hide();
					}
				});
			}else{
				setColPermissions.setDialogHtml('', 'tcolPerm_dataGrid');
			}
		},
		setDialogHtml : function(html,div){
			$("#" + div).html(html);
		},
	    createDialog : function(){
	      this.kindEditerDialog = KindEditor.dialog({
				width : 800,
				height: 550,
				title : '栏目列表',
				body : '<div id="column_source_div" style="padding:10px;height:460px;overflow:auto;overflow-x:hidden;"></div>',
				shadowMode:true,
				closeBtn : {
					name : '关闭',
					click : function(e) {
						setColPermissions.kindEditerDialog.remove();
					}
				},
				yesBtn : {
					name : '保存设置',
					click : function(e) {
						setColPermissions.operationPerm('tcolPerm_dataGrid',0);
					}
				},
				noBtn : {
					name : '解除设置',
					click : function(e) {
						setColPermissions.operationPerm('tcolPerm_dataGrid_right',1);
					}
				}
			});
	    },
	    operationPerm : function(id,type){
	    	var colObj = $("#" + id).find("input[type='checkbox'][name='columnId']:checked");
	    	
			if(colObj.size() > 0){
				if(confirm('确定' + (type == 0?'保存':'解除') + '吗？')){
					var cidStr = "";
					colObj.each(function(){
						cidStr += ";" + $(this).val();
					});
					$.ajax({
						url:'/CMS/main/userColumnOperation',
						type:'post',
						data:{'cidStr':cidStr,'userId': setColPermissions.getUser().val(),'webId':$("#web_select").val(),'type':type},
						dataType:'json',
						success:function(data){
							if(data > 0 ){
								$.growlUI('数据已' + (type == 0?'保存':'解除'));
								$("#" + id).find("input[type='checkbox'][name='columnId']:checked").attr('checked',false);
								setColPermissions.loadPermColumnSource();
							}else{
								$.growlUI('操作失败');
							}
						},
						error:function(msg){
							$.growlUI((type == 0?'保存':'解除') + '出错');
						}
					});
				}
			}else{
				$.growlUI('未选择栏目');
			}
	    },
		getUser:function(){
			return $("input[type='checkbox'][name='web']:checked");
		}
};

